package com.lanhai.longheng.dispatch;
 
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
  
import com.lanhai.longheng.beans.DeviceBean;
import com.lanhai.longheng.beans.DeviceDataBean;
import com.lanhai.longheng.beans.DeviceMountBean;
import com.lanhai.longheng.enums.DeviceDataTypeEnum;
import com.lanhai.longheng.interfaces.IAgv; 
import com.lanhai.longheng.interfaces.IAgvEvent;
import com.lanhai.longheng.interfaces.IAgvMount;
import com.lanhai.longheng.interfaces.ICmdLog;
import com.lanhai.longheng.interfaces.IDevice; 
import com.lanhai.longheng.interfaces.IDeviceMount;
import com.lanhai.longheng.interfaces.IDeviceProtocol;
import com.lanhai.longheng.interfaces.IDot;
import com.lanhai.longheng.interfaces.ISendData;
import com.lanhai.longheng.interfaces.ITempData; 
import com.lanhai.longheng.third.DeviceFactory; 

/**
 * 设备的处理
 * 
 * @author zhengwei
 *
 */
@EnableAsync
@Service
public class DispatchDevice {
 
	
	@Autowired
	IDevice iDevice;
	
	@Autowired
	IDot iDot;
	
	@Autowired
	IAgv iAgv;
	
	@Autowired
	IAgvEvent iAgvEvent;
	
	@Autowired
	IAgvMount iAgvMount;
	
	@Autowired
	IDeviceMount iDeviceMount;
	 
	
	@Autowired
	ITempData iTempData;
	

	@Autowired
	ICmdLog iCmdLog; 
	
	@Autowired
	ISendData sendTools;
	  
	@Async("agvExecutor") 
	public void resolve(String ip,  byte[] data) {
		 
		//可能的设备，主要拿cmdsign 
		 String cmdSign=iDevice.getCmdSign(ip);
		if(cmdSign==null) {
			return;
		}
		
		
		IDeviceProtocol device =DeviceFactory.getDevice(cmdSign);
		if(device==null) {
			return;
		}
		  
		DeviceDataBean bean=device.analyseData(ip, data);
		if(bean==null) {
			//iCmdLog.addDeviceReceiveLog(ip, 0, "无法解析的指令",Tools.byte2HexStr(data));
			return;
		} 
		
	
		// 获取真实的设备
		DeviceBean deviceDao = iDevice.getDao(bean.getIp(),   bean.getLine());
		if (deviceDao == null) {
			return;
		}
		
		 
		
		
		int agvid=iTempData.getAgvIdByDotNo(deviceDao.getDotNO());
		
		
		switch (bean.getEventType()) {
		case AllowLeave: 
			iCmdLog.addDeviceReceiveLog(ip, bean.getLine(), bean.getEventType().getEventName(),bean.getCmdStr(),deviceDao.getDeviceGroup());
			DeviceCmdMap.saveState(deviceDao.getDeviceid() , DeviceDataTypeEnum.AllowLeave); 
			break;
		case Receive:
			iCmdLog.addDeviceReceiveLog(ip, bean.getLine(), bean.getEventType().getEventName(),bean.getCmdStr(),deviceDao.getDeviceGroup());
			DeviceCmdMap.saveState(deviceDao.getDeviceid() , DeviceDataTypeEnum.Receive); 
		case AllowTrans: 
			 //允许传输   
			iCmdLog.addDeviceReceiveLog(ip, bean.getLine(), bean.getEventType().getEventName(),bean.getCmdStr(),deviceDao.getDeviceGroup());
			DeviceCmdMap.saveState(deviceDao.getDeviceid() , DeviceDataTypeEnum.AllowTrans); 
			break; 
		case Info: 
			// 更新装卸情况   
			DeviceMountBean dm=bean.getBean(); 
			if(DeviceCmdMap.isSame(deviceDao.getDeviceid(), bean.getCmdStr())) {
				//不到达临界值，相同的指令仍然过滤
				if(dm.isAllowIn()==false  && dm.isAllowOut() ==false ) {
					return;
				}
			}
			DeviceCmdMap.save(deviceDao.getDeviceid(), bean.getCmdStr());  
			iCmdLog.addDeviceReceiveLog(ip, bean.getLine(), bean.getEventType().getEventName(),bean.getCmdStr(),deviceDao.getDeviceGroup());
			if (AgvCmdMap.isTransing(agvid) == false) {
				// 更新装载情况,传输过程中不更新 
				bean.updateDeviceId(deviceDao.getDeviceid()); 
				iDeviceMount.saveCache(deviceDao.getDeviceid(), dm);
			}
			
			break;
		case Error:
			//机台异常  
			if(DeviceCmdMap.isSame(deviceDao.getDeviceid(), bean.getCmdStr())) {
				return;
			}
			DeviceCmdMap.save(deviceDao.getDeviceid(), bean.getCmdStr());
			iCmdLog.addDeviceReceiveLog(ip, bean.getLine(), bean.getEventType().getEventName(),bean.getCmdStr(),deviceDao.getDeviceGroup());
			iDevice.updateError(deviceDao.getDeviceid() ); 
			break;
		default:
			break;
		}

	}
}
